import requests
from bs4 import BeautifulSoup

class pcScrawler():
    def __init__(self):
        self.headers={
            'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95'
        }
    #爬文本
    def getTargetText(self,url,selector):
        codes=requests.get(url, headers=self.headers).text
        bs = BeautifulSoup(codes,'html.parser')
        return [item.text for item in bs.select(selector=selector)]

    #爬图片
    def getTargetImg(self,imgurl):
        imgc = requests.get(imgurl).content
        with open("2.jpg",'wb') as f:
            f.write(imgc)

if __name__=='__main__':
    url = "https://www.163.com/news/article/I1LN7KOJ000189FH.html"
    s = pcScrawler()
    imgurl = "https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2023%2F0408%2F6dfb4fdfj00rssevc00gpc000n600cyg.jpg&thumbnail=660x2147483647&quality=80&type=jpg"
    print(s.getTargetImg(imgurl))